using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace org.synthful.Win32
{
    public class FileTypeAssociation
    {
        public FileTypeAssociation(string ext)
        {
            this.resolveCommands(ext);
        }

        public string getSubKeyValue(string subkeyname, string valuename)
        {
            return getSubKeyValue(Registry.ClassesRoot, subkeyname, valuename);
        }

        static public string getSubKeyValue(RegistryKey regkey, string subkeyname, string valuename)
        {
            try
            {
                RegistryKey subkey = regkey.OpenSubKey(subkeyname);
                string value = (string)regkey.GetValue(valuename);
                regkey.Close();
                return value;
            }
            catch
            {
                return null;
            }
        }

        public void resolveCommands(string ext)
        {
            try
            {
                RegistryKey subkey = Registry.ClassesRoot.OpenSubKey(ext);
                this.ContentType = (string)subkey.GetValue("ContentType");
                string defaultKeyName = (string)subkey.GetValue("(Default)");
                subkey.Close();

                RegistryKey defaultkey = Registry.ClassesRoot.OpenSubKey(defaultKeyName);

                RegistryKey key = defaultkey.OpenSubKey("");

                RegistryKey iconkey = defaultkey.OpenSubKey("DefaultIcon");
                this.Icon = (string)iconkey.GetValue("(Default)");

                RegistryKey shellkey = defaultkey.OpenSubKey("shell");
                this.OpenerCmd = (string)shellkey.GetValue("open\\command");
                this.ViewCmd = (string)shellkey.GetValue("view\\command");
                this.PrintCmd = (string)shellkey.GetValue("print\\command");
            }
            catch
            {
            }
        }

        public string ContentType;
        public string Icon;
        public string OpenerCmd;
        public string ViewCmd;
        public string EditCmd;
        public string PrintCmd;
    }
}
